package com.team.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.team.entity.Contact;
import com.team.util.DbHelp;
import com.team.util.ResultSetPackge;

public class ContactDao {
	private DbHelp<Contact> db = new DbHelp<Contact>();
	
	public List<Contact> findAllByProjId(int projId){
		String sql = "select id,contactname,companyname,mobelnum,telnum,email,address,weibo,content,t_project_id,websate from t_contact where t_project_id = ?";
		return db.exeSelectForList(sql,new PackgeContact(),projId);
		
	}
	
	 
	
	public int  save(Contact contact){
		String sql = "insert into t_contact(contactname,companyname,mobelnum,telnum,email,address,weibo,websate,content,t_project_id) values(?,?,?,?,?,?,?,?,?,?)";
		return db.executeUpdateSql(sql,contact.getContactName(), contact.getCompanyName(),contact.getMobelNum(),contact.getTelNum(),contact.getEmail(),
				contact.getAddress(),contact.getWeiBo(),contact.getWebSate(),contact.getContent(),contact.getProjId());
		
		
	}

	public Contact selectByContactId(int id) {
		String sql = "select id,contactname,companyname,mobelnum,telnum,email,address,weibo,content,t_project_id,websate from t_contact where id = ?"; 
		return db.exeSelectForObject(sql, new PackgeContact(), id);
	}
	


	/*public List<Contact> findAll() {
		String sql = "select id,contactname,companyname,mobelnum,telnum,email,address,weibo,content,t_project_id,websate from t_contact ";
		return db.exeSelectForList(sql, new PackgeContact());
	
	}*/

	
	private class PackgeContact implements ResultSetPackge<Contact>{

		public Contact packgeResultSet(ResultSet rs) throws SQLException {
			Contact ct = new Contact();
			ct.setAddress(rs.getString("address"));
			ct.setCompanyName(rs.getString("companyname"));
			ct.setContactName(rs.getString("contactname"));
			ct.setContent(rs.getString("content"));
			ct.setEmail(rs.getString("email"));
			ct.setMobelNum(rs.getString("mobelnum"));
			ct.setProjId(rs.getInt("t_project_id"));
			ct.setTelNum(rs.getString("telnum"));
			ct.setWebSate(rs.getString("websate"));
			ct.setWeiBo(rs.getString("weibo"));
			ct.setId(rs.getInt("id"));
			return ct;
		}

	}
}
